package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.UserContext;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.LogEntity;
import com.zhenwei.zscm.mapper.ILogMapper;
import com.zhenwei.zscm.mappings.LogMapping;
import com.zhenwei.zscm.model.dto.log.LogDTO;
import com.zhenwei.zscm.model.dto.user.UserDTO;
import com.zhenwei.zscm.model.vo.log.QueryLogVO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LogRepo extends ServiceImpl<ILogMapper, LogEntity> {
    @Async
    public void createAsync(String logType, String logMessage,String logUser, String logUserId) {
        var e = new LogEntity();
        e.setGuid(IdUtil.simpleUUID());
        e.setLogLevel("INFO");
        e.setLogMessage(logMessage);
        e.setLogType(logType);
        e.setLogTime(new Date());
        e.setLogUser(logUser);
        e.setLogUserId(logUserId);

        this.save(e);
    }
//    @Async
    public void createAsync(String logType, String logMessage) {
        UserDTO currentUser = UserContext.getCurrentUser();

        var e = new LogEntity();
        e.setGuid(IdUtil.simpleUUID());
        e.setLogLevel("INFO");
        e.setLogMessage(logMessage);
        e.setLogType(logType);
        e.setLogTime(new Date());
        e.setLogUser(currentUser == null ? "" : currentUser.getRealName());
        e.setLogUserId(currentUser == null ? "" : currentUser.getGuid());

        this.save(e);
    }
    @Async
    public void createAsync(String logType, String logMessage, String logLevel) {
        UserDTO currentUser = UserContext.getCurrentUser();

        var e = new LogEntity();
        e.setLogLevel(logLevel);
        e.setGuid(IdUtil.simpleUUID());
        e.setLogMessage(logMessage);
        e.setLogType(logType);
        e.setLogTime(new Date());
        e.setLogUser(currentUser == null ? "" : currentUser.getRealName());
        e.setLogUserId(currentUser == null ? "" : currentUser.getGuid());

        this.save(e);
    }

    public List<LogDTO> list(QueryLogVO query){
        var q = new LambdaQueryWrapper<LogEntity>();
        q.eq(StrUtil.isNotBlank(query.getLogType()), LogEntity::getLogType, query.getLogType());
        q.like(StrUtil.isNotBlank(query.getLogMessage()), LogEntity::getLogMessage, query.getLogMessage());
        q.ge(query.getLogTimeStart() != null, LogEntity::getLogTime, query.getLogTimeStart());
        q.le(query.getLogTimeEnd() != null, LogEntity::getLogTime, query.getLogTimeEnd());

        q.orderByDesc(LogEntity::getId);
        var p = new Page<LogEntity>(query.getPageNo(), query.getPageSize(), true);
        var page =this.page(p,q);
        query.setTotalCount(page.getTotal());
        var list = page.getRecords();
        return Linq.mapList(list, LogMapping.INSTANCE::entity2Dto);
    }

    public List<LogDTO> listByType(String logType, int pageNo, int pageSize) {
        var q = new LambdaQueryWrapper<LogEntity>();
        q.eq(LogEntity::getLogType, logType);
        if(UserContext.isNotAdmin()){
            q.eq(LogEntity::getLogUserId, UserContext.getCurrentUser().getGuid());
        }
        q.orderByDesc(LogEntity::getId);
        var p = new Page<LogEntity>(pageNo, pageSize, true);
        var page =this.page(p,q);
        var list = page.getRecords();
        return Linq.mapList(list, LogMapping.INSTANCE::entity2Dto);
    }

    public List<String> getLogTypes(){
        var q = new LambdaQueryWrapper<LogEntity>();
        q.groupBy(LogEntity::getLogType);
        q.select(LogEntity::getLogType);
        q.orderByAsc(LogEntity::getLogType);
        List<LogEntity> list = this.list(q);
        return Linq.mapList(list, LogEntity::getLogType);
    }
}
